//
//  FKBroadcastListVC.m
//  LiveDemo1
//
//  Created by xmc06 on 16/10/13.
//  Copyright © 2016年 zmosa. All rights reserved.
//

#import "FKBroadcastListVC.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "FKLiveItem.h"
#import "FKLiveCell.h"
#import "FKLiveVC.h"

@interface FKBroadcastListVC ()
/** 直播 */
@property (nonatomic, strong) NSMutableArray *lives;
@end

@implementation FKBroadcastListVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"直播列表";
    [self setupSubviews];
    [self loadData];
}

- (void)setupSubviews
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - 网络请求
- (void)loadData
{
    // 印客数据url
    NSString *urlStr = @"http://116.211.167.106/api/live/aggregation?uid=133825214&interest=1";
    
    // 请求数据
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/plain", nil];
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _lives = [FKLiveItem mj_objectArrayWithKeyValuesArray:responseObject[@"lives"]];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.lives.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKLiveCell *liveCell = [FKLiveCell liveCellWithTableView:tableView];
    liveCell.live = self.lives[indexPath.row];
    return liveCell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKLiveVC *liveVC = [[FKLiveVC alloc] init];
    liveVC.live = self.lives[indexPath.row];
    [self presentViewController:liveVC animated:true completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 430;
}

@end
